<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Insect</title>
    <link rel="stylesheet" href="/Trees/LayUiBac/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-container">
    <div class="layui-row" style="border: cadetblue 1px solid;padding: 20px;margin-top: 30px">
        <h1>虫害一览</h1>

        <table id="demo" lay-filter="insectTable"></table>

        <div class="layui-col-md4" style="border: cadetblue 1px solid;padding: 20px">
            <input type="text" name="title" required lay-verify="required" id="insectName" placeholder="请输入你的害虫名称"
                   autocomplete="off" class="layui-input">
            <br>
            <input type="text" name="title" required lay-verify="required" id="host" placeholder="请输入宿主名称"
                   autocomplete="off" class="layui-input">
            <br>
            <button class="layui-btn" type="button" id="Find">查询</button>
            <button class="layui-btn" type="button" id="back">所有</button>
            <button class="layui-btn layui-bg-orange" type="button" id="buttonAdd">添加新的虫害信息</button>
        </div>
    </div>
</div>


<script src="/Trees/LayUiBac/layui/layui.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看详情</a>
</script>
<script>
    layui.use('table', function () {
        let $ = layui.$;
        let layer = layui.layer;
        let table = layui.table;
        //第一个实例
        let show = table.render({
            elem: '#demo'
            , url: '/Trees/insect' //数据接口
            , page: true //开启分页
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "count": res.total, //解析数据长度
                    "data": res.list
                };
            }
            , cols: [[ //表头
                {field: 'pkId', title: 'ID', sort: true, fixed: 'left'}
                , {field: 'name', title: '名称'}
                , {field: 'host', title: '寄主'}
                , {field: 'danger', title: '主要危害'}
                , {fixed: 'right', align: 'center', toolbar: '#barDemo'}
            ]]
        });
        $('#Find').on('click', function () {
            show.reload({
                where: {
                    insectName: $('#insectName').val()
                    , host: $('#host').val()
                }
                , page: {
                    curr: 1
                }
            })
        })

        $('#back').on('click', function () {
            show.reload({
                where: {
                    insectName: ""
                    , host: ""
                }
                , page: {
                    curr: 1
                }
            })
        })

        $('#buttonAdd').on('click', function () {
            let a, b, c, d, e, f, g;
            layer.open({
                type: 2
                , content: "/Trees/LayUiBac/home/insect/Jump/addInsect.html"//填一个url地址
                , area: ['600px', '600px']
                , shade: ['0.9', '#393D49']
                , anim: 0
                , maxmin: true
                , btn: ["确认提交"]
                , yes: function (index, layero) {
                    let body = layer.getChildFrame('body', index);
                    a = body.find("input[name=name]").val();
                    b = body.find("input[name=host]").val();
                    c = body.find("input[name=danger]").val();
                    d = body.find("input[name=breed]").val();
                    e = body.find("input[name=enemy]").val();
                    f = body.find("textarea[name=step]").val();
                    g = body.find("input[name=picUp]").val();
                    let strs = "&insectName=" + a + "&host=" + b + "&danger=" + c + "&breed=" + d + "&enemy=" + e + "&step=" + f + "&picUp=" + g;
                    $.ajax({
                        type: "post",
                        url: "/Trees/insect?method=add" + strs
                    })

                    layer.close(index);
                    show.reload();
                }
                , success: function (layero, index) {

                }

            })
        })


        table.on('tool(insectTable)', function (obj) {
            let data = obj.data;
            console.log(data)
            let layEvent = obj.event;
            let tr = obj.tr;

            if (layEvent === "detail") {
                layer.open({
                    type: 2
                    , content: "/Trees/LayUiBac/home/insect/Jump/showInsect.html"//填一个url地址
                    , area: ['600px', '600px']
                    , shade: ['0.9', '#393D49']
                    , anim: 0
                    , maxmin: true
                    , btn: ["返回主菜单"]
                    , yes: function (index, layero) {
                        // let body = layer.getChildFrame('body',index);
                        layer.close(index);
                    }
                    , success: function (layero, index) {
                        let body = layer.getChildFrame('body', index);
                        body.find("input[name=name]").val(data.name);
                        body.find("input[name=host]").val(data.host);
                        body.find("input[name=danger]").val(data.danger);
                        body.find("input[name=breed]").val(data.breed);
                        body.find("input[name=enemy]").val(data.enemy);
                        body.find("textarea[name=step]").val(data.step);
                        body.find("img[name=picture]").attr('src', data.picture);
                    }
                })
            }
        })

    });


</script>


</body>
</html>